package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import com.ejia.pojo.notice;
import com.ejia.server.MoneyService;
import com.ejia.server.UserService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.SpinnerListModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.TextArea;
import java.awt.Color;
import javax.swing.JSlider;
import javax.swing.JScrollBar;
import javax.swing.JSeparator;
import javax.swing.JToggleButton;
import java.awt.Toolkit;

public class XQGG extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private JTextField txtTitle;
	private static ArrayList<notice> notices;
	private JTextField txtType;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					XQGG frame = new XQGG();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void dateBind() {
		notices = UserService.findNotice();
		Object[][] data = new Object[notices.size()][];
		for (int i = 0; i < data.length; i++) {
			data[i] = new Object[] {
					notices.get(i).getNid(),
					notices.get(i).getTitle(),
					notices.get(i).getNoticetype(),
					notices.get(i).getAddtime()
			};
		}
		table.setModel(new DefaultTableModel(data, new String[] {
				"\u7F16\u53F7", "\u516C\u544A\u6807\u9898", "\u7C7B\u578B", "\u6DFB\u52A0\u65E5\u671F" }));
		table.setRowHeight(40);
		table.setFont(new Font("宋体", Font.PLAIN, 26));
		table.getColumnModel().getColumn(0).setPreferredWidth(20);
		
	}
	

	/**
	 * Create the frame.
	 */
	public XQGG() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(XQGG.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("信息公告");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1334, 711);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 15, 716, 626);
		contentPane.add(scrollPane);

		table = new JTable();
		dateBind();
		table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 26));
		scrollPane.setViewportView(table);

		JLabel lblNewLabel = new JLabel("标题");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel.setBounds(832, 15, 80, 50);
		contentPane.add(lblNewLabel);

		txtTitle = new JTextField();
		txtTitle.setFont(new Font("宋体", Font.PLAIN, 24));
		txtTitle.setBounds(927, 25, 264, 30);
		contentPane.add(txtTitle);
		txtTitle.setColumns(10);
		
		txtType = new JTextField();
		txtType.setFont(new Font("宋体", Font.PLAIN, 24));
		txtType.setBounds(927, 102, 264, 33);
		contentPane.add(txtType);
		txtType.setColumns(10);
		
		TextArea textArea = new TextArea();
		textArea.setRows(200);
		textArea.setFont(new Font("宋体", Font.PLAIN, 24));
		textArea.setBounds(842, 254, 434, 303);
		contentPane.add(textArea);

		JLabel lblNewLabel_1 = new JLabel("类型");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel_1.setBounds(832, 93, 80, 50);
		contentPane.add(lblNewLabel_1);

		JLabel lblNewLabel_2 = new JLabel("内容");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 32));
		lblNewLabel_2.setBounds(832, 165, 80, 50);
		contentPane.add(lblNewLabel_2);
		 
		
		

		JButton button = new JButton("保存");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String title= txtTitle.getText();
				String type= txtType.getText();
				String nr=textArea.getText();
				if (UserService.addNotice(title, type, nr)) {
					dateBind();
					JOptionPane.showMessageDialog(null,"添加成功");
				}else {
					JOptionPane.showMessageDialog(null,"添加失败");
				}
			}
		});
		button.setFont(new Font("宋体", Font.PLAIN, 42));
		button.setBounds(842, 573, 200, 65);
		contentPane.add(button);
		
		JLabel label = new JLabel("请在换行处加上</br>");
		label.setForeground(Color.RED);
		label.setBounds(842, 230, 187, 21);
		contentPane.add(label);
		
		JButton button_1 = new JButton("返回");
		button_1.setFont(new Font("宋体", Font.PLAIN, 42));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_1.setBounds(1076, 573, 200, 65);
		contentPane.add(button_1);
		setLocationRelativeTo(null);
	}
}
